#include "../include/login_page.h"
#include "../include/chat_page.h"
#include "../include/comm.h"

static lv_ft_info_t info;
static lv_style_t style;

static lv_obj_t *ta_username;
static lv_obj_t *ta_password;

void login_page_style_init()
{
    info.name = "/mnt/hgfs/share/simkai.ttf";
    info.weight = 20;
    info.style = FT_FONT_STYLE_NORMAL;
    info.mem = NULL;
    if(!lv_ft_font_init(&info)) {
        LV_LOG_ERROR("create failed.");
    }

    lv_style_init(&style);
    lv_style_set_text_font(&style, info.font);
}

void show_login_page()
{
    login_page_style_init();

    lv_obj_t *login_page = lv_obj_create(lv_scr_act());
    lv_obj_set_size(login_page, 800, 480);

    lv_obj_t *lb_title = lv_label_create(login_page);
    lv_obj_align(lb_title, LV_ALIGN_TOP_MID, 0, 50);
    lv_obj_add_style(lb_title, &style, 0);
    lv_label_set_text(lb_title, "欢迎使用悦聊");

    lv_obj_t *lb_username = lv_label_create(login_page);
    lv_obj_set_pos(lb_username, 190, 160);
    lv_obj_set_size(lb_username, 100, 50);
    lv_obj_add_style(lb_username, &style, 0);
    lv_label_set_text(lb_username, "用户名");

    ta_username = lv_textarea_create(login_page);
    lv_obj_align_to(ta_username, lb_username, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
    lv_obj_set_size(ta_username, 300, 50);
    lv_obj_add_style(ta_username, &style, 0);
    lv_textarea_set_one_line(ta_username, true);
    lv_textarea_set_placeholder_text(ta_username, "请输入用户名");

    // lv_obj_add_event_cb(ta_username, ta_username_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *lb_password = lv_label_create(login_page);
    lv_obj_set_pos(lb_password, 190, 260);
    lv_obj_set_size(lb_password, 100, 50);
    lv_obj_add_style(lb_password, &style, 0);
    lv_label_set_text(lb_password, "密码");

    ta_password = lv_textarea_create(login_page);
    lv_obj_align_to(ta_password, lb_password, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
    lv_obj_set_size(ta_password, 300, 50);
    lv_obj_add_style(ta_password, &style, 0);
    lv_textarea_set_one_line(ta_password, true);
    lv_textarea_set_password_mode(ta_password, true);
    lv_textarea_set_password_show_time(ta_password, 1000);
    lv_textarea_set_placeholder_text(ta_password, "请输入密码");

    // lv_obj_add_event_cb(ta_password, ta_password_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *btn_login = lv_btn_create(login_page);
    lv_obj_set_pos(btn_login, 300, 350);
    lv_obj_set_size(btn_login, 200, 50);

    lv_obj_t *lb_login = lv_label_create(btn_login);
    lv_obj_add_style(lb_login, &style, 0);
    lv_label_set_text(lb_login, "登录");
    lv_obj_center(lb_login);

    lv_obj_add_event_cb(btn_login, login_cb, LV_EVENT_CLICKED, NULL);

    // kb = lv_keyboard_create(admin_login_page);
    // lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
}

void login_cb(lv_event_t *e)
{
    const char *username = lv_textarea_get_text(ta_username);
    const char *password = lv_textarea_get_text(ta_password);

    char sbuf[200] = {0}, rbuf[100] = {0};
    snprintf(sbuf, sizeof(sbuf), "login@%s@%s\n", username, password);

    client_init();
    send(tcpsock, sbuf, strlen(sbuf), 0);

    get_comm_info(rbuf);
    
    if(strcmp(rbuf, "Failed") == 0)
    {
        close(tcpsock);
        static const char *btns[] = {""};
        lv_obj_t *msgbox = lv_msgbox_create(lv_scr_act(), "提示", "用户名或密码错误！", btns, true);
        lv_obj_t *title = lv_msgbox_get_title(msgbox);
        lv_obj_t *text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(title, &style, 0);
        lv_obj_add_style(text, &style, 0);
        lv_obj_center(msgbox);
    }
    else
    {
        strncpy(curr_user, rbuf, sizeof(curr_user));
        pthread_create(&pid_recv, &myattr, client_recv, NULL);
        show_chat_page();
    }
}
